package cloud.maque.sso.server.controller.customer;

import cloud.maque.sso.server.service.PhoneHandle;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.oauth2.template.SaOAuth2Util;
import cn.dev33.satoken.util.SaFoxUtil;
import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson2.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 自定义手机登录接口
 */
@RestController
public class PhoneLoginController {

    @Autowired
    PhoneHandle phoneHandle;

    @PostMapping("/oauth2/sendPhoneCode")
    public SaResult sendCode(@RequestBody JSONObject jsonObject, HttpServletRequest request) {

        SaOAuth2Util.checkClientSecret(request.getParameter("client_id"), request.getParameter("client_secret"));

        String phone = jsonObject.getString("phone");
        String code = phoneHandle.sendCode(phone);
        SaManager.getSaTokenDao().set("phone_code:" + phone, code, 60 * 5);
        System.out.println("手机号：" + phone + "，验证码：" + code + "，已发送成功");
        return SaResult.ok("验证码发送成功");
    }

}